<?php
ini_set('memory_limit','1024M');
header('Access-Control-Allow-Origin:*');
header('Content-Type:application/json');//加上这行,前端那边就不需要var result = $.parseJSON(data);
    require_once('./Classes/PHPExcel.php');//手动引入PHPExcel.php
    require_once('./Classes/PHPExcel/IOFactory.php');//引入IOFactory.php 文件里面的PHPExcel_IOFactory这个类
    $PHPExcel = new \PHPExcel();//实例化

    $PHPSheet = $PHPExcel->getActiveSheet();
    $PHPSheet->setTitle("贵州医科大学2020届毕业生冬季招聘会单位回执单"); //给当前活动sheet设置名称

// 1. 第1和第2行 表头的设置
    $PHPSheet->getStyle('A1')->getFont()->setName('贵州医科大学2020届毕业生冬季校园招聘会参会目录');
    $PHPSheet->getStyle('A1')->getFont()->setSize(18);
    $PHPSheet->getStyle('A1')->getFont()->setBold(true);
    $PHPSheet->getStyle('A2')->getFont()->setName('备注：此参会目录仅供参考，具体以现场为准。为方便统计，若干、不限设置为20个岗位。');
    $PHPSheet->getStyle('A2')->getFont()->setSize(14);
	$PHPSheet->setCellValue("A1",'贵州医科大学2019届毕业生冬季校园招聘会参会目录');
	$PHPSheet->setCellValue("A2",'备注：此参会目录仅供参考，具体以现场为准。为方便统计，若干、不限设置为20个岗位。');

// 2. 数据信息

// 获取数据信息
$list_data =json_decode($_POST['list_data'],true);
if(!$_POST['list_data']){
	$res = ['status'=>'fail','msg'=>'数据传递失败'];
	echo json_encode($res);
	die();
}
// 列表数据信息



// $list_data = [
	
// 	[
// 		['key'=>'某某公司','position'=>'A3&A4'],
// 		['key'=>'贵州省','position'=>'B6'],
// 		['key'=>'贵阳市','position'=>'C6'],
// 		['key'=>'云岩区','position'=>'D6'],
// 		['key'=>'123876AHJSGD6','position'=>'D6'],
// 		['key'=>'小华','position'=>'F6'],
// 		['key'=>'18886036771','position'=>'G6'],
// 		['key'=>'123321@qq.com','position'=>'H6'],
// 		['key'=>'123321@qq.com','position'=>'H6'],
// 		['key'=>'5','position'=>'I6'],
// 		['key'=>'是','position'=>'J6'],
// 		['key'=>'0','position'=>'K6'],
// 		['key'=>'是','position'=>'L6'],
// 		['key'=>'1','position'=>'M6'],
// 		['key'=>'是','position'=>'N6'],
// 		['key'=>'0','position'=>'O6'],
// 		['key'=>'是','position'=>'P6'],
// 		['key'=>'6','position'=>'BM6'],
// 	],
			
// ];



// 数据的赋值
$merge_list = [];
for($j=0;$j<count($list_data);$j++){
	
	for($y=0;$y<count($list_data[$j]);$y++){
		$position = explode('&',$list_data[$j][$y]['position']);
		
		$PHPSheet->setCellValue($position[0]," ".$list_data[$j][$y]['key']);

		$PHPSheet->getStyle($position[0])->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
		// 设置垂直居中 
		$PHPSheet->getStyle($position[0])->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		
		// 合并数组添加元素
		 if(count($position)>1){
			 array_push($merge_list,$position[0].':'.$position[(count($position)-1)]);
		 }
	}
	
	
}

// 合并处理
for($yz=0;$yz<count($merge_list);$yz++){
	$PHPSheet->mergeCells(''.$merge_list[$yz]);
}

//  数据输出
    $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");  //创建生成的格式
    header('Content-Disposition: attachment;filename="贵州医科大学2020届毕业生冬季招聘会单位回执单.xlsx"'); //下载下来的表格名
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件